home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / MailRobot / Implementation / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-19  |  5.0 KB  |  269 lines

  1. #include "listserv.h"
  2. static char rcsid[] = "$Header: /tempf/aem/listserv/RCS/main.c,v 1.2 91/06/17 16:45:39 aem Exp $";
  3.  
  4. FILE *mailer;
  5. FILE *logfile;
  6.  
  7. #include <time.h>
  8.  
  9. main(argc,argv)
  10. int argc;
  11. char **argv;
  12.     {
  13.     char from[256];
  14.     char buf[BUFSIZ];
  15.     char grp[64];
  16.     char dat[64];
  17.     char *p;
  18.     int i;
  19.     int gotcommand = 0;
  20.     int inheader;
  21.  
  22.     /* gotta have some default if the From: line is missing */
  23.     strcpy(from, "Postmaster (no From line)");
  24.  
  25.     inheader = 1;
  26.     while (fgets(buf,BUFSIZ,stdin) != NULL)
  27.         {
  28.         /* drop trailing newline */
  29.         while (buf[(i=strlen(buf)-1)] == '\n')
  30.             buf[i] = '\0';
  31.  
  32.         /* drop trailing blanks */
  33.         p = buf + (strlen(buf) - 1);
  34.         while (p && *p && p >= buf && isspace(*p))
  35.             *p-- = '\0';
  36.  
  37.         /* deblank beginning of line */
  38.         p = buf;
  39.         while (p && *p && isspace(*p))
  40.             p++;
  41.         if (p != buf)
  42.             strcpy(buf, p);
  43.  
  44.         if (strlen(buf) == 0)    /* blank line ends header */
  45.             {
  46.             inheader = 0;
  47.             continue;
  48.             }
  49.         
  50.         /*
  51.         get the From: line so we can return the answer
  52.  
  53.         Note that we DON'T check that we're in the header when
  54.         picking up the From: line; that's so that we'll take the
  55.         last from line we encounter which makes it handle
  56.         forwarded messages correctly.
  57.         */
  58.         if (strncasecmp(buf,"From: ",6) == 0)
  59.             {
  60.             strcpy(from, &buf[6]);
  61.             cleanup(from);
  62.             continue;
  63.             }
  64.         
  65.         /* 
  66.         don't look for commands in the header 
  67.         */
  68.         if (inheader)
  69.             continue;
  70.         
  71.         /* avoid mail loops - ignore requests from ourself! */
  72.         if (!strncasecmp(from, "listserv", 8)
  73.         ||  !strncasecmp(from, "robot", 5))
  74.             {
  75.             printf("I refuse to talk to myself.\n");
  76.             exit(0);
  77.             }
  78.  
  79.         /* force to lower case and scan for commands */
  80.         /* No!  TBL 921019 */
  81. #ifdef NO_CASE_SIGNIFICANCE
  82.         p = buf;
  83.         while (p && *p)
  84.             {
  85.             if (isupper(*p))
  86.                 *p = tolower(*p);
  87.             p++;
  88.             }
  89. #endif
  90.  
  91.         /* If STOP, ignore this and all following commands */
  92.         if (!strcasecmp(buf,"stop")
  93.         ||  !strcasecmp(buf,"end"))
  94.             {
  95.             gotcommand++;
  96.             break;
  97.             }
  98.         
  99.         /* log the request */
  100.         logfile = fopen(LOGFILE,"a");
  101.         if (logfile != NULL)
  102.             {
  103.             long clock = time(0);
  104.             strcpy(dat,ctime(&clock));
  105.             /* drop trailing newline */
  106.             while (dat[(i=strlen(dat)-1)] == '\n')
  107.                 dat[i] = '\0';
  108.             printf("logfile: %s|%s|%s\n", dat, from, buf);
  109.             fprintf(logfile,"%s|%s|%s\n", dat, from, buf);
  110.             fflush(logfile);
  111.             fclose(logfile);
  112.             }
  113.         else
  114.             perror(LOGFILE);
  115.         
  116.         if (!strncasecmp(buf,"add ", 4)
  117.         || !strncasecmp(buf,"subscribe ", 10))
  118.             {
  119.             gotcommand++;
  120.             subscription(from,buf,1);
  121.             continue;
  122.             }
  123.         
  124.         if (!strncasecmp(buf,"delete ", 7)
  125.         || !strncasecmp(buf,"unsubscribe ", 12))
  126.             {
  127.             gotcommand++;
  128.             subscription(from,buf,0);
  129.             continue;
  130.             }
  131.         
  132.         if (!strcasecmp(buf,"index")
  133.         || !strcasecmp(buf,"longindex"))
  134.             {
  135.             gotcommand++;
  136.             sendindex(from,buf,!strcasecmp(buf,"longindex"));
  137.             continue;
  138.             }
  139.         
  140.         if (!strncasecmp(buf,"www ", 4)
  141.         || !strncasecmp(buf,"send ", 5))
  142.             {
  143.             gotcommand++;
  144.             senddoc(from,buf);
  145.             continue;
  146.             }
  147.         
  148.         if (!strcasecmp(buf,"help"))
  149.             {
  150.             gotcommand++;
  151.             sendhelp(from,buf);
  152.             continue;
  153.             }
  154.         
  155.         if (!strncasecmp(buf,"help ",5))
  156.             {
  157.             gotcommand++;
  158.             sscanf(buf,"%*s%s", grp);
  159.             listhelp(from,grp,buf);
  160.             continue;
  161.             }
  162.         }
  163.     if (gotcommand == 0)
  164.         sendhelp(from, "indecipherable");
  165.     }
  166.  
  167.  
  168. /*        Invoke MAILER to send reply message
  169. **        -----------------------------------
  170. */
  171. callmailer(redirect, toaddr, request)
  172. char *redirect, *toaddr, *request;
  173.     {
  174.     char cbuf[BUFSIZ];
  175.  
  176.     printf("callmailer \"%s\",\"%s\",\"%s\"\n", redirect, toaddr, request);
  177.  
  178.     strcpy(cbuf, MAILER);
  179.     strcat(cbuf, " -f");
  180.     strcat(cbuf, POSTMASTER);
  181.     strcat(cbuf, " -F\"Mail robot\"");
  182.     strcat(cbuf, " -t -oi"); 
  183.     strcat(cbuf, " "); strcat(cbuf, redirect);
  184.     mailer = popen(cbuf,"w");
  185.     printf("mailer popen '%s'\n", cbuf);
  186.     if (mailer == NULL)
  187.         {
  188.         perror(cbuf);
  189.         exit(1);
  190.         }
  191.     fprintf(mailer,"From: %s (Mail robot)\n",POSTMASTER);
  192.     fprintf(mailer,"To: %s\n", toaddr);
  193.     fprintf(mailer,"Subject: Re: %s\n", request);
  194.     fprintf(mailer,"\n");
  195.     }
  196.  
  197. mailcat(mfname,prefix)
  198. char *mfname, *prefix;
  199.     {
  200.     FILE *mf;
  201.     char buf[BUFSIZ];
  202.  
  203.     printf("mailcat \"%s\", \"%s\"\n", mfname, prefix);
  204.  
  205.     mf = fopen(mfname,"r");
  206.     if (mf == NULL)
  207.         {
  208.         perror(mfname);
  209.         fprintf(mailer,"This should have been the contents of\n");
  210.         fprintf(mailer,"file '%s' but it's missing!\n",mfname);
  211.         return;
  212.         }
  213.  
  214.     /* copy the message file into the mailer */
  215.     while (fgets(buf,BUFSIZ,mf) != NULL)
  216.         {
  217.         fputs(prefix,mailer);
  218.         fputs(buf,mailer);
  219.         }
  220.     fclose(mf);
  221.     return;
  222.     }
  223.  
  224. cleanup(from)
  225. char *from;
  226.     {
  227.     char *p, *q;
  228.  
  229.     printf("cleanup \"%s\"\n", from);
  230.  
  231.     while (p = index(from,'~'))
  232.         *p = 'X';
  233.  
  234.     while (p = index(from,'/'))
  235.         *p = 'X';
  236.  
  237.     while (p = index(from,'|'))
  238.         *p = 'X';
  239.  
  240.     /* elide stuff in parenthesis */
  241.     if (p = index(from,'('))
  242.         {
  243.         if ( (q=index(from,')')) == NULL)
  244.             {
  245.             /* zap the from line; it's invalid */
  246.             *p = '\0';
  247.             return;
  248.             }
  249.         strcpy(p, q+1);
  250.         }
  251.  
  252.     if (p = index(from,'<'))
  253.         {
  254.         if ( (q=index(from,'>')) == NULL)
  255.             {
  256.             /* zap the from line; it's invalid */
  257.             *p = '\0';
  258.             return;
  259.             }
  260.         *q = '\0';
  261.         strcpy(from, p+1);
  262.         }
  263.  
  264.     /* drop trailing blanks */
  265.     p = from + (strlen(from) - 1);
  266.     while (p && *p && p >= from && isspace(*p))
  267.         *p-- = '\0';
  268.     }
  269.